package com.suptrack.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.suptrack.entity.User;

/**
 * Servlet Filter implementation class AdminFilter
 */
@WebFilter("/RegisteredContent/Admin/*")
public class AdminFilter implements Filter {

	private FilterConfig config;

	public void destroy() {
		this.config = null;
	}

	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if(request instanceof HttpServletRequest ){
			 
			 HttpServletRequest req = (HttpServletRequest) request;
			 HttpServletResponse resp=(HttpServletResponse) response;
			 
			 HttpSession Session= req.getSession();	
			User filterUser= (User) Session.getAttribute("User");
		
			 	if( filterUser==null  || ( filterUser !=null && filterUser.isAccType()==false ) ){
			 		
			
			 		resp.sendRedirect( config.getServletContext().getContextPath()+"/index.jsp");
			 		
			 		
				
			return;
			 	}
		}
		chain.doFilter(request, response);
	}

	
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}

}
